package mapEditor.view.panels
{
	import asset.mapEditor.panelToolAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	import mapEditor.vo.ToolType;

	public class PanelToolsView extends panelToolAsset
	{
		private var _model:EditorModel;
		
		public function PanelToolsView(model:EditorModel)
		{
			_model=model;
			setView();
			setEvent();
		}
		
		private function setView():void
		{
			resetTool();
			toolSelect.gotoAndStop(2);
		}
		
		private function setEvent():void
		{
			addEventListener(Event.ADDED_TO_STAGE, __onAddedToStage);
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			_model.addEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			toolSelect.addEventListener(MouseEvent.CLICK, __onToolClick);
			toolMove.addEventListener(MouseEvent.CLICK, __onToolClick);
			toolZoomIn.addEventListener(MouseEvent.CLICK, __onToolClick);
			toolZoomOut.addEventListener(MouseEvent.CLICK, __onToolClick);
		}
		
		private function removeEvent():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, __onAddedToStage);
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			_model.removeEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			toolSelect.removeEventListener(MouseEvent.CLICK, __onToolClick);
			toolMove.removeEventListener(MouseEvent.CLICK, __onToolClick);
			toolZoomIn.removeEventListener(MouseEvent.CLICK, __onToolClick);
		}
		
		private function __onMouseOver(evt:MouseEvent):void
		{
			Mouse.cursor=MouseCursor.AUTO;
		}
		
		private function __onAddedToStage(evt:Event):void
		{
			stage.addEventListener(Event.RESIZE, __onResize);
			toolBg.height=stage.stageHeight-x;
		}
		
		private function __onResize(evt:Event):void
		{
			toolBg.height=stage.stageHeight-x;
		}		
		
		private function __onToolClick(evt:MouseEvent):void
		{
			resetTool();
			switch(evt.currentTarget)
			{
				case toolSelect:
					toolSelect.gotoAndStop(2);
					_model.toolType=ToolType.TOOL_SELECT;
					break;
				case toolMove:
					toolMove.gotoAndStop(2);
					_model.toolType=ToolType.TOOL_MOVE;
					break;
				case toolZoomIn:
					toolZoomIn.gotoAndStop(2);
					_model.toolType=ToolType.TOOL_ZOOM_IN;
					break;
				case toolZoomOut:
					toolZoomOut.gotoAndStop(2);
					_model.toolType=ToolType.TOOL_ZOOM_OUT;
					break;
			}
		}
		
		/**
		 *当操作工具类型改变时 
		 */		
		private function __onToolTypeChange(evt:MapEditorEvent):void
		{
			switch(evt.date.toString())
			{
				case ToolType.TOOL_MOVE:
					resetTool();
					toolMove.gotoAndStop(2);
					break;
				case ToolType.TOOL_SELECT:
					resetTool();
					toolSelect.gotoAndStop(2);
					break;
				case ToolType.TOOL_ZOOM_IN:
					resetTool();
					toolZoomIn.gotoAndStop(2);
					break;
				case ToolType.TOOL_ZOOM_OUT:
					resetTool();
					toolZoomOut.gotoAndStop(2);
					break;
				default:
					resetTool();
					break;
			}
		}
		
		private function resetTool():void
		{
			toolSelect.gotoAndStop(1);
			toolMove.gotoAndStop(1);
			toolZoomIn.gotoAndStop(1);
			toolZoomOut.gotoAndStop(1);
		}
		
		public function dispose():void
		{
			removeEvent();
			
			if(parent) parent.removeChild(this);
		}
	}
}